﻿using CQIE.TES.IDAL;
using CQIE.TES.Model;
using CQIE.TES.WEB.USER.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace CQIE.TES.DAL
{
    public class Class_DAL : DALBase<Class_>, IClass_DAL
    {
        /// <summary>
        /// 获取所有班级
        /// </summary>
        /// <returns></returns>
        public List<Class_> GetAllClasses()
        {
            using (MyDB db = new MyDB())
            {
                var list = db.Class_.ToList();
                return list;
            }
        }

        /// <summary>
        /// 分页、查询实现
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="search"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Class_> GetClass_Page(int page, int limit, string search, out int count, ENUM_Graduate root)
        {

            
            using (MyDB db = new MyDB())
            {
                
                List<Class_> query;
                if (root == ENUM_Graduate.All)
                {
                    query = (from cla in db.Class_
                             where cla.ClassCode.Contains(search)
                             orderby cla.Grade descending //必须使用排序才能使用Skip（）,Take()方法
                             select cla).ToList();
                    
                }
                else if (root == ENUM_Graduate.No)
                {
                    query = (from cla in db.Class_
                             where cla.ClassCode.Contains(search)&&cla.IsGraduate==false
                             orderby cla.Grade descending //必须使用排序才能使用Skip（）,Take()方法
                             select cla).ToList();
                }
                else
                {
                    query = (from cla in db.Class_
                             where cla.ClassCode.Contains(search) && cla.IsGraduate == true
                             orderby cla.Grade descending //必须使用排序才能使用Skip（）,Take()方法
                             select cla).ToList();
                }
                count = query.Count;
                //获取所有班级条目数
                query = query.Skip(page * limit).Take(limit).ToList();
                var query_id = (from cla in db.Class_
                                select cla);

                foreach (var item in query_id)
                {
                    DateTime nowTime = DateTime.Now;
                    var model_grade = db.Class_.Find(item.ID);
                    //设置毕业时间为四年
                    string temp = $"{model_grade.Grade + 4}-06-01 12:00:00";
                    DateTime endTime = DateTime.Parse(temp);
                    //当现在的时间超过毕业时间更改数据库里面的字段IsGraduate为true
                    //nowTime>=endTime
                    if (nowTime.CompareTo(endTime) >= 0)
                    {
                        //根据id查询数据
                        var model = db.Class_.Find(item.ID);
                        //更改是否毕业
                        model.IsGraduate = true;
                    }

                }
                //更新到数据库
                db.SaveChanges();

                var data = query.Select(cla => new Class_()
                {
                    ID = cla.ID,
                    ClassCode = cla.ClassCode,
                    Grade = cla.Grade,
                    Major = cla.Major,
                    IsGraduate = cla.IsGraduate

                }).ToList();
                return data;

            }
        }

        /// <summary>
        /// 是否毕业
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteClass_(int id)
        {
            using (MyDB db = new MyDB())
            {
                try
                {
                    //查找数据
                    var model_delete = db.Class_.Find(id);
                    //删除数据
                    db.Class_.Remove(model_delete);
                    //更新数据库
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 新增班级
        /// </summary>
        /// <param name="data"></param>
        public void InsertClass_(string classCode,int grade,string major)
        {

            using (MyDB db = new MyDB())
            {
                //最后一个序列的id加1
                //var lastIdAndOne = db.Class_.Last().ID + 1;
                db.Class_.Add(new Class_()
                {
                    ClassCode = classCode,
                    Grade = grade,
                    Major = major,
                    IsGraduate = false
                });


                var query_id = (from cla in db.Class_
                                select cla);

                foreach (var item in query_id)
                {
                    DateTime nowTime = DateTime.Now;
                    var model_grade = db.Class_.Find(item.ID);
                    //设置毕业时间为四年
                    string temp = $"{model_grade.Grade + 4}-06-01 12:00:00";
                    DateTime endTime = DateTime.Parse(temp);
                    //当现在的时间超过毕业时间更改数据库里面的字段IsGraduate为true
                    //nowTime>=endTime
                    if (nowTime.CompareTo(endTime) >= 0)
                    {
                        //根据id查询数据
                        var model = db.Class_.Find(item.ID);
                        //更改是否毕业
                        model.IsGraduate = true;
                    }
                    //根据id查询数据
                    //var remodel = db.Class_.Find(id);
                    //return remodel.IsGraduate;

                }
                //再次保存数据
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 通过学生ID获取他的班级
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public Class_ FindClassByStundentID(int sid)
        {
            using (MyDB db = new MyDB())
            {
                var model = db.Student.FirstOrDefault(s => s.ID == sid);
                return model.Class_;
            }
        }

        public Class_ FindClassByStudent(int sid)
        {
            using (MyDB db = new MyDB())
            {
                var student = db.Student.FirstOrDefault(s => s.ID == sid);
                return student.Class_;
            }
        }

        public List<Class_> GetClassIDToStudent()
        {
            using(MyDB db = new MyDB())
            {
                var query = (from cla in db.Class_
                            select cla).ToList();

                var data = query.Select(cla => new Class_()
                {
                    ID = cla.ID,
                    ClassCode = cla.ClassCode
                }).ToList();
                return data;
                
            }
        }
    }
}

